package com.futureCloudMOM.core.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.futureCloudMOM.common.exception.base.BaseException;
import com.futureCloudMOM.core.base.service.impl.BaseServiceImpl;
import com.futureCloudMOM.core.domain.ProduceTaskBillMaterial;
import com.futureCloudMOM.core.domain.TechnologyBomSub;
import com.futureCloudMOM.core.mapper.ProduceTaskBillMaterialMapper;
import com.futureCloudMOM.core.service.ProduceTaskBillMaterialService;
import com.futureCloudMOM.core.service.TechnologyBomSubService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import java.util.List;

/**
 * 【Produce_TaskBillMaterial】的数据库操作Service实现
 *
 * @author Administrator
 * @since 2023-06-06 16:03:04
 */
@Service
@AllArgsConstructor
public class ProduceTaskBillMaterialServiceImpl extends BaseServiceImpl<ProduceTaskBillMaterialMapper, ProduceTaskBillMaterial> implements ProduceTaskBillMaterialService {

    private final TechnologyBomSubService technologyBomSubService;

    @Override
    public List<ProduceTaskBillMaterial> selectListByBillId(Long billId) {
        return list(new LambdaQueryWrapper<ProduceTaskBillMaterial>()
                .eq(ProduceTaskBillMaterial::getTaskBillId, billId));
    }

    @Override
    public void create(Long billId, Long bomId) {
        if (ObjectUtils.isEmpty(bomId)) {
            return;
        }
        List<TechnologyBomSub> bomSubs = technologyBomSubService.selectListByMainId(bomId);
        if (CollectionUtils.isEmpty(bomSubs)) {
            throw new BaseException("未查询到工艺BOM数据");
        }
        bomSubs.forEach(technologyBomSub -> {
            ProduceTaskBillMaterial taskBillMaterial = ProduceTaskBillMaterial.getInstance(technologyBomSub);
            taskBillMaterial.setTaskBillId(billId);
            save(taskBillMaterial);
        });
    }

    @Override
    public void deleteByBillId(Long billId) {
        getBaseMapper().delete(new LambdaQueryWrapper<ProduceTaskBillMaterial>()
                .eq(ProduceTaskBillMaterial::getTaskBillId, billId));
    }

}
